﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace Maydew.Windows.Forms.Controls.FlexiCell
{
    /// <summary>
    /// 为 ColumnHeader 类提供集合管理。
    /// </summary>
    public class ColumnHeaderCollection : System.Collections.CollectionBase
    {
        #region " 事件 "
        /// <summary>
        /// 当集合改变后发生。
        /// </summary>
        public event CollectionChangeEventHandler Changed;
        #endregion

        #region " 私有变量 "
        /// <summary>
        /// 获取绘图面板。
        /// </summary>
        private Control _canvas;
        #endregion

        #region " 构造函数 "
        /// <summary>
        /// 初始化 ColumnHeaderCollection 类的新实例。
        /// </summary>
        public ColumnHeaderCollection(Control canvas)
        {
            _canvas = canvas;
        }
        #endregion

        #region " 索引器 "
        /// <summary>
        /// 获取或设置列标题。
        /// </summary>
        /// <param name="index">索引。</param>
        /// <returns>返回 ColumnHeader 对象。</returns>
        public ColumnHeader this[int index]
        {
            get
            {
                return (ColumnHeader)List[index];
            }
            set
            {
                List[index] = value;
            }
        }
        /// <summary>
        /// 获取列标题。
        /// </summary>
        /// <param name="columnName">列名。</param>
        /// <returns>返回 ColumnHeader 对象。</returns>
        public ColumnHeader this[string columnName]
        {
            get
            {
                for (int i = 0; i < Count; i++)
                {
                    if (string.Compare(columnName, this[i].ColumnName) == 0)
                    {
                        return this[i];
                    }
                }
                return null;
            }
        }
        #endregion

        #region " 属性 "
        /// <summary>
        /// 获取绘图面板。
        /// </summary>
        public Control Canvas
        {
            get
            {
                return _canvas;
            }
        }
        #endregion

        #region " 方法 "
        /// <summary>
        /// 添加列标题。
        /// </summary>
        /// <param name="header">列标题。</param>
        /// <returns>返回插入后的索引。</returns>
        public int Add(ColumnHeader header)
        {
            return List.Add(header);
        }
        /// <summary>
        /// 插入列标题。
        /// </summary>
        /// <param name="index">要插入的索引。</param>
        /// <param name="header">列标题。</param>
        public void Insert(int index, ColumnHeader header)
        {
            List.Insert(index, header);
        }
        /// <summary>
        /// 移除列标题。
        /// </summary>
        /// <param name="header">列标题。</param>
        public void Remove(ColumnHeader header)
        {
            List.Remove(header);
        }
        /// <summary>
        /// 获取列标题的索引。
        /// </summary>
        /// <param name="header">列标题。</param>
        /// <returns>成功返回列标题索引，否则，返回 -1。</returns>
        public int IndexOf(ColumnHeader header)
        {
            return List.IndexOf(header);
        }
        /// <summary>
        /// 获取列标题的索引。
        /// </summary>
        /// <param name="columnName">列名。</param>
        /// <returns>成功返回列标题索引，否则，返回 -1。</returns>
        public int IndexOf(string columnName)
        {
            return IndexOf(this[columnName]);
        }
        /// <summary>
        /// 获取列标题是否包含在集合中。
        /// </summary>
        /// <param name="header">列标题。</param>
        /// <returns>成功返回 True，否则，返回 False。</returns>
        public bool Contains(ColumnHeader header)
        {
            return List.Contains(header);
        }
        #endregion

        #region " 友元方法 "
        /// <summary>
        /// 当插入列标题时发生。
        /// </summary>
        /// <param name="index">要插入的索引。</param>
        /// <param name="value">列标题。</param>
        protected override void OnInsert(int index, object value)
        {
            ColumnHeader header = (ColumnHeader)value;
            header.Canvas = _canvas;

            base.OnInsert(index, value);

            OnChanged(new CollectionChangeEventArgs(CollectionChangeAction.Add, value));
        }
        /// <summary>
        /// 当移除列标题时发生。
        /// </summary>
        /// <param name="index">要移除的索引。</param>
        /// <param name="value">列标题。</param>
        protected override void OnRemove(int index, object value)
        {
            ColumnHeader header = (ColumnHeader)value;
            header.Canvas = null;

            base.OnRemove(index, value);

            OnChanged(new CollectionChangeEventArgs(CollectionChangeAction.Remove, value));
        }
        /// <summary>
        /// 当验证列标题是发生。
        /// </summary>
        /// <param name="value">列标题。</param>
        protected override void OnValidate(object value)
        {
            if (value.GetType() != typeof(ColumnHeader))
                throw new ArgumentException("value must be of type 'ColumnHeader'.", "value");
            base.OnValidate(value);
        }
        /// <summary>
        /// 当集合改变后发生。
        /// </summary>
        /// <param name="e">集合改变参数。</param>
        protected virtual void OnChanged(CollectionChangeEventArgs e)
        {
            if (Changed != null) Changed(this, e);
        }
        #endregion
    }
}
